package com.yingpai.fitness.dvp_2_0.main.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.ActivityManager;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.HomeActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.LogisticsBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.DashedLineView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看物流
 * Created by ASUS on 2018/11/21.
 */

public class LogisticsActivity extends CommonActivity {

    @BindView(R.id.listview)
    ListView listview;
    private QuickAdapter<LogisticsBean.DataBeanX.DataBean> quickAdapter;
    private TextView express;
    private TextView waybill_number;
    private String com;
    private String num;
    //7=已出库
    private String state;
    @BindView(R.id.viewstub)
    ViewStub viewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_activity_logistics);
    }

    @Override
    protected void initView() {
        setTitle("物流信息");

        Bundle bundle = getIntent().getExtras();
        if (bundle!=null) {
            com = bundle.getString("com");
            num = bundle.getString("num");
            state = bundle.getString("state");
        }

        View view = View.inflate(this, R.layout.head_view_logistics, null);
        express = view.findViewById(R.id.express);
        waybill_number = view.findViewById(R.id.waybill_number);


        quickAdapter = new QuickAdapter<LogisticsBean.DataBeanX.DataBean>(this, R.layout.item_logistics) {
            @Override
            protected void convert(BaseAdapterHelper helper, LogisticsBean.DataBeanX.DataBean item) {
                DashedLineView up_line = helper.getView(R.id.up_line);
                DashedLineView down_line = helper.getView(R.id.down_line);
                View line = helper.getView(R.id.line);
                ImageView point = helper.getView(R.id.point);
                LinearLayout right_ll = helper.getView(R.id.right_ll);

                helper.setText(R.id.message,item.getContext());
                helper.setText(R.id.time,item.getFtime());

                if(helper.getPosition()==0){
                    up_line.setVisibility(View.INVISIBLE);
                    point.setImageResource(R.mipmap.point_yellow);
                }else{
                    up_line.setVisibility(View.VISIBLE);
                    point.setImageResource(R.mipmap.point_gray);
                }

                down_line.setMinimumHeight(right_ll.getMeasuredHeight()-point.getMeasuredHeight());

                if(helper.getPosition()==quickAdapter.getCount()-1){
                    down_line.setVisibility(View.INVISIBLE);
                    line.setVisibility(View.GONE);
                }else{
                    line.setVisibility(View.VISIBLE);
                    down_line.setVisibility(View.VISIBLE);
                }

            }
        };
        listview.setAdapter(quickAdapter);
        listview.addHeaderView(view);
    }

    @Override
    protected void initData() {
        if(state .equals("7")){
            if(viewStub != null){
                View stubView = viewStub.inflate();
                listview.setVisibility(View.GONE);
                stubView.findViewById(R.id.gotomall).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(HomeActivity.class);
                        ActivityManager.getActivityManager().popAllActivityExceptOne(HomeActivity.class);
                    }
                });
            }
        }else{
            showDialog(this,null);
            HashMap<String,Object> map = new HashMap<>();
            map.put("com",com);
            map.put("num",num);
            finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.GET_LOGISTICS_INFO, map, Url.GET_LOGISTICS_INFO, new Callback<LogisticsBean>() {
                @Override
                public void onSuccess(LogisticsBean logisticsBean) {
                    if(logisticsBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                        express.setText(logisticsBean.getData().getComName());
                        waybill_number.setText("运单号：" + logisticsBean.getData().getNu());

                        quickAdapter.clear();
                        quickAdapter.addAll(logisticsBean.getData().getData());
                        quickAdapter.notifyDataSetChanged();
                    }else{
                        showToast(logisticsBean.getMessage()+"");
                    }
                    dismissDialog();
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    dismissDialog();
                }
            });
        }

    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }
}
